package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.entity.PmsBrand;
import com.shop.feign.FileService;
import com.shop.mapper.PmsBrandMapper;
import com.shop.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author lpx610
 * @since 2023-11-02
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Resource
    private FileService fileService;

    @Override
    public IPage<PmsBrand> list(Integer pageNo, Integer pageSize, String name, Integer order) {
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (order == 1) {
            queryWrapper.orderByAsc("sort");
        } else {
            queryWrapper.orderByDesc("sort");
        }
        return this.page(new Page<>(pageNo, pageSize), queryWrapper);
    }

    @Override
    public List<PmsBrand> getName() {
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name");
        return this.list(queryWrapper);
    }

    @Override
    public Boolean add(String name, String description, MultipartFile file) {
        String logo = fileService.uploadLogo(file);
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setName(name)
                .setDescription(description)
                .setLogo(logo);
        return this.save(pmsBrand);
    }

    @Override
    public Boolean update(String id, String name, String description, MultipartFile file) {
        PmsBrand pmsBrand = new PmsBrand(id);
        pmsBrand.setName(name)
                .setDescription(description);
        if (file != null) {
            String logo = fileService.uploadLogo(file);
            pmsBrand.setLogo(logo);
        }
        return this.updateById(pmsBrand);
    }

    @Override
    public Boolean delete(String id, Boolean active) {
        return this.updateById(new PmsBrand(id).setActive(active));
    }
}
